<%@page import="com.ks.utils.StringUtil"%>
<%@page import="common.Var"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%-- <!-- 不自动创建session -->
<%@ page session="false"%> --%>
<%@ page import="java.util.Date"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login JSP</title>
<script src="../static/js/jquery-3.3.1.js" type="text/javascript"></script>
<script src="../static/js/md5.js" type="text/javascript"></script>
</head>
<body>

	<%-- 	<%
		/*
		先获取request中所有cookie对象，如果cookie中存在名字为username和password的cookie，
		就将cookie的值传到LoginServlet进行登录验证。这样一来，只要曾经成功登录过，那么一小时之内就不需要重新登录。
		*/
		String username = null;
		String password = null;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (Var.USERNAME.equals(cookie.getName())) {
					username = cookie.getValue();
				}
				if (Var.PASSWORD.equals(cookie.getName())) {
					password = cookie.getValue();
				}
			}
			if (username != null && password != null) {
				request.getRequestDispatcher("/login?username=" + username + "&password=" + password)
						.forward(request, response);
				return;

			}
		} else {
			out.println("<h1>No Cookie!</h1> 请登录");
		}
	%> --%>


	<%-- 	<%
		//产生随机数，和密码一起生成MD5  
		request.getSession().setAttribute("md5RandomKey", StringUtil.getRandomNum(8));
	%> --%>
	<form action="${pageContext.request.contextPath}/login" method="post" onsubmit="encrypt()">
		<!-- 	用户名：<input type="text" id="username" name="username" /> <br /> 密码：<input
			type="password" id="password" name="password" /> <br /> <input
			type="checkbox" name="rememberMe" id="rememberMe" value="true"
			onclick="remember();" />记住密码 <br /> <input type="submit" value="登录">
			
			 -->
		<table border="1" width="550px" cellpadding="0" cellspacing="0"
			align="center">
			<tr>
				<td height="35" align="center">用户名</td>
				<td>&nbsp;&nbsp;&nbsp; <input type="text" id="username"
					name="username" />
				</td>
			</tr>
			<tr>
				<td height="35" align="center">密 &nbsp; 码</td>
				<td>&nbsp;&nbsp;&nbsp; <input type="password" id="password"
					name="password" />
				</td>
			</tr>
			<tr>
				<td height="35" align="center">记住密码</td>
				<td>&nbsp;&nbsp;&nbsp; <input type="checkbox" name="rememberMe"
					id="rememberMe" value="true" onclick="remember();" />
				</td>

			</tr>
			<tr><td height="35" align="center">密码记住时长</td>
				<td><input type="radio" name="timeout" value="-1" checked>
					关闭浏览器即失效 <br /> <input type="radio" name="timeout"
					value="<%=30 * 24 * 60 * 60%>"> 30天 内有效 <br /> <input
					type="radio" name="timeout" value="<%=Integer.MAX_VALUE%>">
					永久有效 <br /></td>
			</tr>
			<tr>
				<td height="35" colspan="3" align="center"><input type="submit"
					value="登录" /> &nbsp;&nbsp;&nbsp;&nbsp; <input type="reset"
					name="重置" /> <a href="/jsp/register.jsp">&nbsp;&nbsp;&nbsp;&nbsp;注册</a>
				</td>
			</tr>
		</table>
	</form>


	<script>
		$(document).ready(
				function() {
					//记住密码功能
					var username = getCookie("username");
					var password = getCookie("password");
					//自动填充用户名和密码
					$("#username").val(username);
					$("#password").val(password);
					if (username != null && username != "" && password != null
							&& password != "") {
						$("input[type='checkbox']").attr("checked", true);
					}
				});

		//获取cookie
		function getCookie(cname) {
			var name = cname + "=";
			var ca = document.cookie.split(';');
			for (var i = 0; i < ca.length; i++) {
				var c = ca[i];
				while (c.charAt(0) == ' ')
					c = c.substring(1);
				if (c.indexOf(name) != -1)
					return c.substring(name.length, c.length);
			}
			return "";
		}

		function remember() {
			var remFlag = $("input[type='checkbox']").is(':checked');
			if (remFlag == true) { //如果选中设置remFlag为true
				//cookie存用户名和密码,回显的是真实的用户名和密码,存在安全问题.
				var conFlag = confirm("记录密码功能不宜在公共场所(如网吧等)使用,以防密码泄露.您确定要使用此功能吗?");
				if (conFlag) { //确认标志
					$("#remFlag").val(true);

				} else {
					$("input[type='checkbox']").prop('checked', false)
					$("#remFlag").val(false);
				}
			} else { //如果没选中设置remFlag为false
				$("#remFlag").val(false);
			}
		}
		//密码加密传输
		function encrypt() {
			if (getCookie("password")==null ||getCookie("password")=="") {
				/* 加盐 使md5更健壮这个最好是从服务端动态获取 */
				var salt = "!@#$%^&*()_+";
				var pass = $("#password").val();
				pass = hex_md5(pass+salt);
				pass = hex_md5(pass+salt);
				$("#password").val(pass);
			}
		}
	</script>


</body>
</html>